Skip to content

feat(cloudflare): Propagate traceparent to RPC calls - via fetch#19991

Open
JPeer264 wants to merge 3 commits intodevelopfrom
jp/rpc-instrument
Open

feat(cloudflare): Propagate traceparent to RPC calls - via fetch#19991
JPeer264 wants to merge 3 commits intodevelopfrom
jp/rpc-instrument

Conversation

@JPeer264
Copy link
Copy Markdown
Member

@JPeer264 JPeer264 commented Mar 26, 2026

relates to #19327

related to #16898 (it is not really closing it as we just add context propagation without adding spans for individual calls. It needs to be defined if we need it)

It is important to know that these RPC calls do only work with the .fetch call:

const id = env.MY_DURABLE_OBJECT.idFromName('workflow-test');
const stub = env.MY_DURABLE_OBJECT.get(id);

await stub.fetch(new Request('http://my-worker/my-do-call'));

This adds RPC fetch calls between:

This works by instrumenting env (via instrumentEnv), which then goes over the bindings and see if there is a DurableObject or a normal Fetcher (full list of current bindings: https://developers.cloudflare.com/workers/runtime-apis/bindings/). This got inspired by how otel-cf-workers instruments their env: https://github.com/evanderkoogh/otel-cf-workers/blob/effeb549f0a4ed1c55ea0c4f0d8e8e37e5494fb3/src/instrumentation/env.ts

With this PR I added a lot of tests to check if trace propagation works (so this PR might look like it added a lot of LoC, but it is mostly tests). So I added it for schedule and queue, but it is not possible for email and tail with wrangler dev.

Potential things to change

Trace propagagtion

I added the addTraceHeaders.ts helper, as there is currently no way to reuse the existing logic (it is baked-in into the fetch instrumentations). It would be nice once #19960 lands that we can reuse it in Cloudflare to reuse existing code. I tried to write couple of tests so we don't have duplicated headers.

Adding extra spans

So there is actually a guide by OTel to add RPC spans, but was talking with someone from the OTel maintainers and they meant that this wouldn't be necessary as we already have an http.server span from out instrumented DurableObjects (and other resources) - so it wouldn't add much of information.

Without RPC span:

Screenshot 2026-03-25 at 10 59 01

With RPC span:

Screenshot 2026-03-25 at 10 55 48

@JPeer264 JPeer264 requested review from Lms24, logaretm and timfish March 26, 2026 14:38
@JPeer264 JPeer264 self-assigned this Mar 26, 2026
@linear-code
Copy link
Copy Markdown

linear-code bot commented Mar 26, 2026

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (cloudflare) Propagate traceparent to RPC calls - via fetch by JPeer264 in #19991
  • (core, node) Portable Express integration by isaacs in #19928
  • (deno) Add denoRuntimeMetricsIntegration by chargome in #20023
  • (deps) Bump @xmldom/xmldom from 0.8.3 to 0.8.12 by dependabot in #20066

Bug Fixes 🐛

  • (aws-serverless) Add timeout to _endSpan forceFlush to prevent Lambda hanging by logaretm in #20064
  • (cloudflare) Ensure every request instruments functions by JPeer264 in #20044
  • (gatsby) Fix errorHandler signature to match bundler-plugin-core API by JPeer264 in #20048

Internal Changes 🔧

Core

  • Extract shared endStreamSpan for AI integrations by nicohrubec in #20021
  • Remove provider-specific AI span attributes in favor of gen_ai attributes in sentry conventions by nicohrubec in #20011

Other

  • Update validate-pr workflow by stephanie-anderson in #20072
  • Remove unused tsconfig-template folder by mydea in #20067

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2026

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 25.65 kB +0.02% +5 B 🔺
@sentry/browser - with treeshaking flags 24.14 kB +0.03% +5 B 🔺
@sentry/browser (incl. Tracing) 42.19 kB +0.11% +44 B 🔺
@sentry/browser (incl. Tracing, Profiling) 46.81 kB +0.1% +45 B 🔺
@sentry/browser (incl. Tracing, Replay) 80.97 kB +0.05% +33 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.59 kB +0.05% +34 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 85.69 kB +0.05% +37 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 97.95 kB +0.04% +36 B 🔺
@sentry/browser (incl. Feedback) 42.42 kB +0.02% +6 B 🔺
@sentry/browser (incl. sendFeedback) 30.31 kB +0.02% +6 B 🔺
@sentry/browser (incl. FeedbackAsync) 35.29 kB +0.02% +6 B 🔺
@sentry/browser (incl. Metrics) 26.96 kB +0.03% +7 B 🔺
@sentry/browser (incl. Logs) 27.11 kB +0.03% +7 B 🔺
@sentry/browser (incl. Metrics & Logs) 27.78 kB +0.03% +7 B 🔺
@sentry/react 27.41 kB +0.03% +6 B 🔺
@sentry/react (incl. Tracing) 44.52 kB +0.1% +42 B 🔺
@sentry/vue 30.08 kB +0.02% +5 B 🔺
@sentry/vue (incl. Tracing) 44.09 kB +0.1% +42 B 🔺
@sentry/svelte 25.67 kB +0.02% +5 B 🔺
CDN Bundle 28.32 kB +0.03% +6 B 🔺
CDN Bundle (incl. Tracing) 43.15 kB +0.12% +51 B 🔺
CDN Bundle (incl. Logs, Metrics) 29.68 kB +0.02% +4 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 44.2 kB +0.1% +43 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 68.48 kB +0.01% +6 B 🔺
CDN Bundle (incl. Tracing, Replay) 80.03 kB +0.05% +36 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 81.09 kB +0.07% +50 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 85.58 kB +0.05% +35 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.61 kB +0.04% +32 B 🔺
CDN Bundle - uncompressed 82.68 kB +0.03% +22 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 127.93 kB +0.1% +127 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 86.83 kB +0.03% +22 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 131.34 kB +0.1% +127 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 209.81 kB +0.02% +22 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.81 kB +0.06% +127 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 248.21 kB +0.06% +127 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 257.72 kB +0.05% +127 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 261.11 kB +0.05% +127 B 🔺
@sentry/nextjs (client) 46.93 kB +0.09% +41 B 🔺
@sentry/sveltekit (client) 42.66 kB +0.1% +39 B 🔺
@sentry/node-core 55.77 kB +0.04% +17 B 🔺
@sentry/node 172.13 kB -0.37% -625 B 🔽
@sentry/node - without tracing 96.05 kB +0.05% +40 B 🔺
@sentry/aws-serverless 112.85 kB +0.07% +78 B 🔺

View base workflow run

Copy link
Copy Markdown
Collaborator

@timfish timfish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@JPeer264 JPeer264 changed the title feat(cloudflare): Propagate traceparent to RPC calls feat(cloudflare): Propagate traceparent to RPC calls - via fetch Mar 31, 2026
@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 71a3936 to 3097f54 Compare March 31, 2026 11:25
@Lms24
Copy link
Copy Markdown
Member

Lms24 commented Mar 31, 2026

It would be nice once #19960 lands that we can reuse it in Cloudflare to reuse existing code. I tried to write couple of tests so we don't have duplicated headers.

FYI, #19960 landed and I agree! Right now, our various request instrumentations combine the "logic"/"algorithm" code with platform specific code how to set headers. I think it would be good if we could strip out the algorithm part into a common helper, and only leave the platform-specific parts to the individual instrumentations. From my PoV a good improvement but something we can also tackle at a later point. Happy to leave this up to you :)

Comment on lines +29 to +31
} else if (!existing.split(',').some(entry => entry.trim().startsWith(SENTRY_BAGGAGE_KEY_PREFIX))) {
headers.set('baggage', `${existing},${baggage}`);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: can we reuse mergeBaggageHeaders here?
Update: realized this is only exported from @sentry/node-core but I think that's actually worth moving to core because it should be fairly platform-independent. WDYT?

Comment on lines +20 to +21

if (!headers.has('sentry-trace')) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: As pointed out in #19960, we should only attach any of our headers if sentry-trace isn't set yet. So we could most likely just early return here.

Comment on lines +13 to +15
const newInit = addTraceHeaders(input, init);

return fetchFn(input, newInit);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m/q: is this how we add our tracing headers in our core/browser fetch instrumentation as well? Just wanted to double check because injecting headers into fetch arguments is a bit tricky with the various forms of input and init and the precedence.

I think adding headers to init is fine. Though us deep-copying headers from input to init, and then init again might be unexpected if one of the objects is reused by users across multiple requests. Anyway, not saying something is off but that we should double check that this is how we do it in the other SDKs as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, that would be indeed a bad behavior - I'll change the behavior to match the others

@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 3097f54 to fb6a835 Compare April 1, 2026 12:34
return init || {};
}

const headers = new Headers(init?.headers || (input instanceof Request ? input.headers : undefined));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The addTraceHeaders function drops headers from an incoming Request if the init object contains an empty but truthy Headers object, due to incorrect override logic.
Severity: HIGH

Suggested Fix

Modify addTraceHeaders to merge headers from both the input (if it's a Request) and init.headers. Instead of simply prioritizing init.headers, create a new Headers object from the input.headers first, and then append headers from init.headers to it. This ensures that headers from both sources are preserved.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/cloudflare/src/utils/addTraceHeaders.ts#L19

Potential issue: When `addTraceHeaders` is called with a `Request` object containing
headers and an `init` object with an empty `Headers` instance (e.g., `{ headers: new
Headers() }`), the function incorrectly prioritizes the empty headers. The logic at line
19 uses `init?.headers` if it's truthy, which an empty `Headers` object is.
Consequently, when the underlying `fetch` is called, the empty headers from `init`
override the original `Request` headers, causing critical headers like `Authorization`
to be silently dropped.

Did we get this right? 👍 / 👎 to inform future reviews.

@JPeer264
Copy link
Copy Markdown
Member Author

JPeer264 commented Apr 1, 2026

@Lms24 I just reworked the way how tracing headers are generated. I actually exported getTracingHeadersForFetchRequest from core, which is used inside instrumentFetchRequests in core. I actually saw that mergeBaggageHeaders is not applied there, but I think this should be added (maybe in a follow up PR?!).

I can also outsource getTracingHeadersForFetchRequest into its own PR if wanted, to make this PR a little bit more focused on one thing. (Edit: I actually outsourced this: #20076)

@JPeer264 JPeer264 requested a review from Lms24 April 1, 2026 15:19
@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 77c7260 to ef05c42 Compare April 1, 2026 15:20
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,861 - 11,213 -21%
GET With Sentry 1,769 20% 1,972 -10%
GET With Sentry (error only) 6,195 70% 7,611 -19%
POST Baseline 1,198 - 1,281 -6%
POST With Sentry 588 49% 627 -6%
POST With Sentry (error only) 1,055 88% 1,132 -7%
MYSQL Baseline 3,255 - 3,494 -7%
MYSQL With Sentry 482 15% 456 +6%
MYSQL With Sentry (error only) 2,641 81% 2,814 -6%

View base workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants